<html>
	<body>
		<embed id="plugin" type="application/dbusmdns" width=0 height=0>
	
		<script>
			var plugin=document.getElementById("plugin");
			var httpServices={};
			var listeners=[];

			function notifyListeners() {
				for (var p=0; p<listeners.length; p++) {
					try {
						var port=listeners[p];
						port.postMessage(httpServices);
					} catch(err) {
						//hmmm... shouldn't occur						
					}
				}//for
			}
			
			setInterval(function(){
				var msg=plugin.popmsg();
				try {
					var jobj = eval("jobj="+msg);
				} catch(err) {
					var jobj = {};
				};
				jobj= jobj || {};
				var signalName = jobj.signal || null;
				if (null==signalName)
					return;

				var signal=signalName.toLowerCase();
				
				if (signal=="itemnew") {
					console.log("new services, name: "+jobj["name"]);
					httpServices[jobj["name"]]=jobj;
				}

				if (signal=="itemremove") {
					console.log("service removed, name: "+jobj["name"]);
					delete httpServices[jobj["name"]];
				}

				notifyListeners();
				console.log( httpServices );
			},1*1000);
			
			chrome.extension.onConnect.addListener(function(port) {
				console.log("onConnect, tabid: "+port.tab.id);
				listeners[port.tab.id] = port;
				notifyListeners();
				
				port.onDisconnect.addListener(function(port) {
					console.log("onDisconnect, tabid: "+port.tab.id);
					delete listeners[port.tab.id];
				});
			});
		
			chrome.browserAction.onClicked.addListener(function(tab) {
				var url=chrome.extension.getURL("services.html");
				chrome.tabs.create({"url":url});
			});
		</script>
	</body>
</html>